package thread;

public class Dome105_Create {
    public static long count = 10_0000;
    public static void main(String[] args) {
        //串行
        serial();
        //并行
        concurrency();
        //获取当前类名、方法名和线程名
        getName();
    }
    //串行
    public static void serial() {
        //获取开始时间
        long begin = System.currentTimeMillis();
        long a = 0;
        for (int i = 0; i < count; i++) {
            a++;
        }
        long b = 0;
        for (int i = 0; i < count; i++) {
            b++;
        }
        //获取结束时间
        long end = System.currentTimeMillis();
        System.out.println("串行耗时：" + (end - begin));
    }
    //并行
    public static void concurrency() {
        //获取开始时间
        long begin = System.currentTimeMillis();
        //创建线程01
        Thread thread01 = new Thread(() -> {
            long a = 0;
            for (int i = 0; i < count; i++) {
                a++;
            }
        });
        //启动线程01
        thread01.start();
        //创建线程02
        Thread thread02 = new Thread(() -> {
            long b = 0;
            for (int i = 0; i < count; i++) {
                b++;
            }
        });
        //启动线程02
        thread02.start();
        //等待线程01和线程02运行结束
        try {
            thread01.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        try {
            thread02.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //获取结束时间
        long end = System.currentTimeMillis();
        System.out.println("并行耗时：" + (end - begin));
    }

    public static void getName() {
    //创建线程t1
    Thread t1 = new Thread(() -> {
    while(true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //获取线程名
        String tName = Thread.currentThread().getName();
        //获取方法名
        String mName = Thread.currentThread().getStackTrace()[1].getMethodName();
        //获取类名
        String cName = Dome105_Create.class.getName();
        System.out.println("当前类名：" + cName + ",当前方法名：" + mName + ",当前线程名：" + tName);
    }
},"拉布拉多");
        //启动线程t1
        t1.start();
   }
}
